package com.paomi.client.fragment.show;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.fragment.show.presenter.ShowFraPre;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Util;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * Created by pc on 2018/10/25
 * Describe:分类中的演出列表
 * com.paomi.client.fragment
 */
public class ShowFragment extends BaseLCEFragment<TicketJson, ShowFraPre> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    @BindView(R.id.fab_ptr)
    FloatingActionButton fab_ptr;
    private String getData = "";
    private String classifyid = "0";
    private boolean get;//是否刷新
    private int pageNum = 1;
    private String check;
    private List<TicketJson> ticketJsonList=new ArrayList<>();

    public static ShowFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        ShowFragment fragment = new ShowFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        fab_ptr.setVisibility(View.VISIBLE);
        setPtrFrameLayout(ptrRefresh);
        //网络请求
        getPresenter().setShowData(getActivity(),pageNum, getData, classifyid);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return recyclerView;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_classify_show;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setTextSize(15);
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_category_empty, 0, 0);
        empty.setText(getResources().getString(R.string.empty_msg));
    }

    @Override
    protected void convert(BaseViewHolder helper, TicketJson item) {
        RoundedImageView performShow = helper.itemView.findViewById(R.id.perform_show);
        Util.setWidthAndHeight(performShow, -1, (int) (Util.getDisplay(getActivity()).widthPixels * 0.4));
        GlideApp.with(this).load(item.getThumb()).placeholder(R.mipmap.img_default_goods).into(performShow);
        helper.setText(R.id.show_title, item.getName());
        helper.setText(R.id.show_date, item.getDate());
        helper.setText(R.id.show_site, item.getVenue_name());
        helper.setText(R.id.show_money, "¥" + item.getPrice());
        helper.setText(R.id.show_num, item.getPay_number() + "人付款");
        switch (item.getTicket_state()){//0是即将开票，1是预售中，2售票中，3演出结束，4已售罄，5扫尾票
            case 0://即将开票
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticket_action));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 1:
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticketing_begin));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
            case 2:
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticketing));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
            case 3://演出结束
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticket_un));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 4:
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticket_finish));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 5://扫尾票
                helper.setText(R.id.show_btn, getResources().getString(R.string.ticket_none));
                helper.setTextColor(R.id.show_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.show_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
        }
        helper.addOnClickListener(R.id.show_btn);
    }

    @Override
    protected void onItemClick(View view, TicketJson item) {
        Intent intent = new Intent(getActivity(), DetailTicketActivity.class);
        intent.putExtra("getId", "" + item.getId());
        startActivity(intent);
    }

    @Override
    public ShowFraPre createPresenter() {
        return new ShowFraPre();
    }

    public void setListData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            getData = bundle.getString("getData");
            classifyid = bundle.getString("classify_id");
            get = bundle.getBoolean("classify_get");
            check = bundle.getString("checkBox");
            Log.e("check", check);
            if (get) {
                pageNum=1;
                //网络请求
                getPresenter().setShowData(getActivity(),pageNum, getData, classifyid);
            }

        }
    }

    @Override
    public void setData(List<TicketJson> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        ticketJsonList.clear();
        ticketJsonList.addAll(data);
        if (data.size() % Constants.PAGE_SIZE !=0) {//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        }else{
            getmAdapterWrapper().setLoadItemVisibility(true);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }

    /**
     * 下拉刷新
     * @param pullToRefresh
     */
    @Override
    public void loadData(boolean pullToRefresh) {
        super.loadData(pullToRefresh);
        if(pullToRefresh){
            pageNum=1;
            getPresenter().setShowData(getActivity(),pageNum, getData, classifyid);
        }
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoad() {
        super.onLoad();
        if(ticketJsonList.size()%Constants.PAGE_SIZE==0){
            pageNum++;
            getPresenter().setShowData(getActivity(),pageNum, getData, classifyid);
        }else{
            getmAdapterWrapper().setLoadItemVisibility(false);
            getmLoadMoreHelper().setLoadMoreFinish();
        }
    }
    @OnClick(R.id.fab_ptr)
    public void onViewClicked() {
        RxToast.info(getResources().getString(R.string.null_msg));
    }
}
